package controller.remote;

import com.zuche.framework.common.IpLocalContext;
import com.zuche.framework.exception.BusinessRuntimeException;
import com.zuche.framework.tradingPlatform.RequestParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 雷霆 on 2016/10/21.
 */
public class CartpRemoteClient {
    private static final Logger logger = LoggerFactory.getLogger(CartpRemoteClient.class);
    private static final String RPC_TARGET = "cartp.rpcEntry.trading";
    private static final String CART_BIND_QUERY = "cartp.creditcardbind.query";
    private static final String CART_BIND_QUERY_ID = "cartp.creditcardbind.queryId";
    private static final String creditcardbind_report = "cartp.creditcardbind.report";
    private static final String MVC_TARGET = "/remoteView.do_?remoteViewService=cartp.mvcEntry.trading&needSession=false&";

    public static Map<String, String> doRpcInvoke(Map<String, String> paramMap,Boolean isTest) {

        Map<String, String> resultMap = new HashMap<String,String>();

        try {
            resultMap= (Map<String, String>) RemoteClientUtils.getCarTpTestClient().executeToObject(RPC_TARGET,paramMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return resultMap;
    }

    public static ArrayList doRpcQueryBindCard(Map<String, String> paramMap,Boolean isTest) {
        ArrayList resultMap = new ArrayList();

        try {
            resultMap= (ArrayList) RemoteClientUtils.getCarTpTestClient().executeToObject(CART_BIND_QUERY,paramMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return resultMap;
    }


    public static Object doRpcQueryBindCardByOperId(String payerId,Boolean isTest) {

        try {
            return RemoteClientUtils.getCarTpTestClient().executeToObject(CART_BIND_QUERY_ID,payerId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return null;
    }


    public static Map<String, Object> doRemote(Map<String, Object> paramMap) {

        Map<String, Object> resultMap = new HashMap<String,Object>();

        try {
            resultMap= (Map<String, Object>) RemoteClientUtils.getCarTpTestClient().executeToObject(creditcardbind_report,paramMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return resultMap;
    }

    /**
     * 发起MVC模式的交易服务请求.
     * <br/> Created on 2013-11-25 上午11:29:36
     * @author  李青原(liqingyuan1986@aliyun.com)
     * @since 3.2.9
     * @param request		HTTP请求
     * @param response		HTTP响应
     * @param paramMap		请求参数MAP
     * @param isTest		是否为测试请求
     */
    public static void doMvc(HttpServletRequest request,HttpServletResponse response, Map<String,String> paramMap, Boolean isTest){
        Map<String,String> resultMap = new HashMap<String,String>();
        resultMap.put(RequestParams.IP, IpLocalContext.get());
        resultMap.put(RequestParams.IS_TEST, isTest.toString());
        resultMap.putAll(paramMap);

        StringBuilder builder = new StringBuilder(MVC_TARGET);
        for(String key : resultMap.keySet()){
            builder.append("&").append(key).append("=").append(resultMap.get(key));
        }
        try{
            request.getRequestDispatcher(builder.toString()).forward(request, response);
        }catch(Exception e){
            throw new BusinessRuntimeException("交易平台MVC模式调用出错！", e);
        }
    }
}
